<template>
  <!--用户管理-->
  <div class="search">
    <a-drawer :visible="modalVisible" :title="modalTitle" width="800" @close="handleClose">
      <a-tabs @tabClick="handleTabClick"  :defaultActiveKey="current" v-model="current">
        <a-tab-pane tab="用户信息" key="form1">
          <user-form ref="form1" v-if="modalVisible"></user-form>
        </a-tab-pane>
        <a-tab-pane tab="分配角色" key="form2" v-if="isEdit">
          <user-role ref="form2" v-if="modalVisible"></user-role>
        </a-tab-pane>
        <a-tab-pane tab="分配权限" key="form3" v-if="isEdit">
          <user-auth ref="form3" v-if="modalVisible"></user-auth>
        </a-tab-pane>
        <a-tab-pane tab="修改密码" key="form4" v-if="isEdit">
          <update-password ref="form4" v-if="modalVisible"></update-password>
        </a-tab-pane>
      </a-tabs>
      <div class="drawer-footer">
        <a-button type="default" @click="handleClose">取消</a-button>&nbsp;
        <a-button type="primary" @click="handleSubmit" :loading="saving">保存</a-button>
      </div>
    </a-drawer>
  </div>
</template>
<script>

  import UserForm from './user_form'
  import UserRole from './user_role'
  import UserAuth from './user_auth'
  import UpdatePassword from './update_password'

  export default {
    name: 'userModal',
    props: {},
    data () {
      return {
        saving: false,
        modalVisible: false,
        isEdit: false,
        title: '',
        current: 'form1',
        userName: '',
        formData: {},
        forms: [
          'form1',
          'form2',
          'form3',
          'form4'
        ]
      }
    },
    methods: {
      handleShow (data) {
        this.isEdit = false
        this.userName = ''
        if (data && data.userId) {
          this.isEdit = true
          this.userName = data.userName
          this.formData = data
        } else {
          this.formData = {}
        }
        this.modalVisible = true
        this.handleTabClick('form1')
      },
      handleClose () {
        //this.$refs[this.current].handleReset()
        this.modalVisible = false
      },
      handleSubmit () {
        this.$refs[this.current].handleSubmit(this.handleClose)
      },
      handleTabClick (name) {
        this.current = name
        if (this.current === this.forms[0]) {
          this.title = this.isEdit ? '编辑用户 - ' : '添加用户'
        }
        if (this.current === this.forms[1]) {
          this.title = '分配角色'
        }
        if (this.current === this.forms[2]) {
          this.title = '分配权限'
        }
        if (this.current === this.forms[3]) {
          this.title = '修改密码'
        }
        this.$nextTick(() => {
          this.$refs[this.current].setData(this.formData)
        })
      }
    },
    computed: {
      modalTitle: function () {
        return this.title + this.userName
      }
    },
    created () {
    },

    components: { UpdatePassword, UserAuth, UserRole, UserForm }
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
